<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小同住人员物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\contract;

use think\App;
use app\controller\property\Base;
use app\common\services\CacheService;
use app\validate\contract\ContractLesseeValidate;
use app\common\repositories\room\RoomBasicRepository;
use app\common\repositories\contract\ContractLesseeRepository;
use app\common\repositories\property\basic\PropertyBasicRepository;


class ContractLessee extends Base
{

    protected $repository;

    public function __construct(App $app, ContractLesseeRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
        $this->assign([
            'addAuth' => property_auth('propertyContractLesseeAdd'),
            'editAuth' => property_auth('propertyContractLesseeEdit'),
            'delAuth' => property_auth('propertyContractLesseeDel'),
            'detailAuth' => property_auth('propertyContractLesseeDetail'),
        ]);
    }


    /**
     * 同住人员列表
     * @return string|\think\response\Json
     * @throws \Exception
     *
     * @author Amos
     * 2024-02-06 22:00
     */
    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'contract_id' => '',
                'keywords' => ''
            ]);
            [$page, $limit] = $this->getPage();
            $data = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $data['list'], 'count' => $data['count']]);
        }
        return $this->fetch('contract/lessee/list');
    }

    /**
     * 添加同住人员
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-02-06 22:00
     */
    public function add()
    {
        $contractId = $this->request->param('contract_id', '0');
        if ($this->request->isAjax() && $this->request->isGet() && $this->request->param('get_type') == 'roomData') {
            $propertyId = $this->request->param('property_id');
            /** @var RoomBasicRepository $roomRepository */
            $roomRepository = app()->make(RoomBasicRepository::class);
            $roomData = $roomRepository->getSearch([])
                ->where('status', 2)
                ->where('property_id', $this->request->propertyId)
                ->field('id,status,contract_id,unit_id,floor_name,room_name')
                ->append(['full_room_name'])
                ->select()->toArray();
            return json()->data(['code' => 0, 'data' => $roomData]);
        } else if ($this->request->isPost()) {
            $param = $this->request->param([
                'contract_id' => '',
                'username' => '',
                'mobile' => '',
                'is_type' => '',
                'number' => '',
                'remark' => '',
                'front_file' => '',
                'back_file' => '',
            ]);
            if ($param['contract_id'] <= 0 && $contractId > 0) {
                $param['contract_id'] = $contractId;
            }
            validate(ContractLesseeValidate::class)->scene('add')->check($param);
            try {
                $res = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加同住人员失败', $e);
                return $this->error('网络失败');
            }
        } else {
            return $this->fetch('contract/lessee/add', [
                'contractId' => $contractId,
            ]);
        }
    }

    /**
     * 编辑同住人员
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-02-06 22:00
     */
    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'username' => '',
                'mobile' => '',
                'is_type' => '',
                'number' => '',
                'remark' => '',
                'front_file' => '',
                'back_file' => '',
            ]);
            validate(ContractLesseeValidate::class)->scene('edit')->check($param);
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('编辑同住人员失败', $e);
                return $this->error('网络错误');
            }
        } else {
            return $this->fetch('contract/lessee/edit', [
                'info' => $info,
            ]);
        }
    }

    /**
     * 同住人员详情
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-02-06 22:01
     */
    public function detail()
    {
        $id = (int)$this->request->param('id');
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if ($info['id'] <= 0) {
            return $this->error('信息不存在');
        }
        return $this->fetch('contract/lessee/detail', [
            'info' => $info,
        ]);
    }

    /**
     *  删除同住人员
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-02-06 22:01
     */
    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除同住人员失败', $e);
            return $this->error('网络失败');
        }
    }

}